package com.gec.oasys.controller;

import com.gec.oasys.pojo.SysUser;
import com.gec.oasys.service.SysUserService;
import com.gec.oasys.util.SoulPage;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;
    @RequestMapping("/findSysUserById")
    @ResponseBody
    public Map<String, Object> findSysUserById(Integer id){
        HashMap<String, Object> mapData = new HashMap<>();
        String code="1",msg="内部错误";
        SysUser sysUser=sysUserService.selectByPrimaryKey(id);
        code="0";msg="成功";
        mapData.put("code",code);
        mapData.put("msg",msg);
        mapData.put("data",sysUser);
        return mapData;
    }
    @RequestMapping("/toSysUserList")
    public String toSysUserList(){
        return "sysuser/sysuserlist";
    }
    @RequestMapping("/deleteSysUserById")
    @ResponseBody
    public Map<String,Object> deleteSysUserById(Integer id){
        HashMap<String, Object> mapData = new HashMap<>();
        String code="1",msg="内部错误";
        int pos =sysUserService.deleteByPrimaryKey(id);
        if(pos>0){
            code="0";msg="成功";
        }
        mapData.put("msg",msg);
        mapData.put("code",code);
        return mapData;
    }
    @RequestMapping("/deleteSysUsersById")
    @ResponseBody
    public Map<String,Object> deleteSysUsersById(String ids){
        HashMap<String, Object> mapData = new HashMap<>();
        String code="1",msg="内部错误";
        String strids[]=ids.split(",");
        int pos =sysUserService.deletesByPrimaryKey(strids);
        if(pos>0){
            code="0";msg="成功";
        }
        mapData.put("msg",msg);
        mapData.put("code",code);
        return mapData;
    }
    @RequestMapping("/toSysUserAdd")
    public String toSysUserAdd(){
        return "sysuser/sysuseradd";
    }
    @RequestMapping("/saveSysUser")
    @ResponseBody
    public Map<String,Object> saveSysUser(SysUser sysUser){
        Map<String,Object> mapData=new HashMap<>();
        String code="1",msg="内部错误";
        int pos=sysUserService.insertEncodePassword(sysUser);
        if(pos>0){
            code="0";msg="成功";
        }
        mapData.put("msg",msg);
        mapData.put("code",code);
        return mapData;
    }
    @RequestMapping("/updateBySysUserPrimaryKey")
    @ResponseBody
    public Map<String,Object> updateByPrimaryKey(SysUser sysUser){
        Map<String,Object> mapData=new HashMap<>();
        String code="1",msg="内部错误";
        int pos=sysUserService.updateByPrimaryKey(sysUser);
        if(pos>0){
            code="0";msg="成功";
        }
        mapData.put("msg",msg);
        mapData.put("code",code);
        return mapData;
    }
    @RequestMapping("/getDataBySysUser")
    @ResponseBody
    public Object dataGrid(SoulPage<SysUser> soulPage, SysUser sysUser) {
        soulPage.setObj(sysUser);
        return sysUserService.dataGrid(soulPage);
    }
    @RequestMapping("/tologinform")
    public String tologinform(){
        return "sysuser/loginform";
    }
    @RequestMapping("/login")
    public String login(SysUser sysUser, HttpServletRequest httpServletRequest){
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken usernamePasswordToken=new UsernamePasswordToken(sysUser.getLoginName(),sysUser.getPwd());
        String result="0";
        try {
            subject.login(usernamePasswordToken);
        }catch (AuthenticationException e){
            System.out.println(e.getMessage());
            httpServletRequest.setAttribute("msg",e.getMessage());
            result="1";
        }
        if(result.equals("1")){
            return "sysuser/loginform";
        }else{
            return "home";
        }
    }


}



